package br.com.unifebe.tcc.browse;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.table.TableCellEditor;

import br.com.unifebe.tcc.UTILS.Icones;
import br.com.unifebe.tcc.componente.GUI.JCFButton;
import br.com.unifebe.tcc.componente.GUI.TableModel;

@SuppressWarnings("serial")
public abstract class BrowseGUI <E extends Object> extends JDialog{

	private JPanel panelGeral; 
	private JPanel panelPesquisa;
	private JPanel panelCentral;
	private JPanel panelToolBar;

	private JLabel labelPesquisa;
	private JTextField editPesquisa;
	private JCFButton botaoPesquisar;

	private JCFButton botaoFechar;
	private JCFButton botaoIncluir;
	private JCFButton botaoEditar;
	private JCFButton botaoExcluir;
	
	private JCFButton botaoAtualizar;
	
	private JTable tablePesquisa;
	private Window owner ;
	
	public BrowseGUI( Window owner ) {
		
		super(owner, Dialog.ModalityType.APPLICATION_MODAL);
		this.owner = owner; 
		
		try {
			UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );
//			UIManager.setLookAndFeel(new NimbusLookAndFeel());
//			UIManager.setLookAndFeel( ch.randelshofer.quaqua.QuaquaManager.getLookAndFeel() );
		} catch (Exception e) {
			e.printStackTrace();
		}
		SwingUtilities.updateComponentTreeUI(this);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setCursor(new java.awt.Cursor(Cursor.DEFAULT_CURSOR));
		pack();
		setSize(new Dimension(400,500));
		setContentPane(getPanelGeral());
		this.setLocationRelativeTo(null);
		getDados();
	}

	public JCFButton getBotaoFechar(){
		if(botaoFechar == null){
			botaoFechar = new JCFButton();
			botaoFechar.setText("Fechar");
			botaoFechar.setIcon(new ImageIcon(getClass().getResource("/imagens/cancelar2.png")));
			botaoFechar.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					dispose();
				}
			});
		}
		return botaoFechar;
	}

	public JCFButton getBotaoIncluir(){
		if(botaoIncluir == null){
			botaoIncluir = new JCFButton();
			botaoIncluir.setIcon( new Icones().iconeIncluir() );
			botaoIncluir.addActionListener( new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					getDados();
					
				}
			});
		}
		return botaoIncluir;
	}

	public JCFButton getBotaoEditar(){
		if(botaoEditar == null){
			botaoEditar = new JCFButton();
			botaoEditar.setIcon( new Icones().iconeEditar() );
		}
		return botaoEditar;
	}
	
	public JCFButton getBotaoExcluir(){
		if(botaoExcluir == null){
			botaoExcluir = new JCFButton();
			botaoExcluir.setIcon( new Icones().iconeExcluir() );
		}
		return botaoExcluir;
	}
	
	public JCFButton getBotaoAtualizar(){
		if(botaoAtualizar == null){
			botaoAtualizar = new JCFButton();
			botaoAtualizar.setIcon( new Icones().iconeBuscar() );
			botaoAtualizar.addActionListener( new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					getDados();
				}
			});
		}
		return botaoAtualizar;
	}
	
	public JCFButton getBotaoPesquisar(){
		if(botaoPesquisar == null){
			botaoPesquisar = new JCFButton();
			botaoPesquisar.setIcon(new ImageIcon(getClass().getResource("/imagens/busca.png")));
		}
		return botaoPesquisar;
	}

	public JLabel getLabelPesquisa(){
		if(labelPesquisa == null ){
			labelPesquisa = new JLabel("Pesquisar: ");
		}
		return labelPesquisa;
	}
	public JTextField getEditPesquisa(){
		if(editPesquisa == null){
			editPesquisa = new JTextField();
		}
		return editPesquisa;
	}

	public JPanel getPanelGeral(){
		if(panelGeral == null){

			panelGeral = new JPanel(new BorderLayout());

			panelGeral.add(getPanelPesquisa(),BorderLayout.NORTH);
			panelGeral.add(getPanelCentral(),BorderLayout.CENTER);
			panelGeral.add(getPanelToolBar(),BorderLayout.SOUTH);

			panelGeral.setBorder(BorderFactory.createEtchedBorder());
		}
		return panelGeral;
	}

	public JPanel getPanelPesquisa(){
		if(panelPesquisa == null){

			panelPesquisa = new JPanel(new BorderLayout());

			panelPesquisa.add(getLabelPesquisa(),BorderLayout.WEST);
			panelPesquisa.add(getEditPesquisa(), BorderLayout.CENTER);
			panelPesquisa.add(getBotaoPesquisar(), BorderLayout.EAST);

			panelPesquisa.setBorder(BorderFactory.createEtchedBorder());
		}
		return panelPesquisa;
	}

	public JPanel getPanelToolBar(){
		if(panelToolBar == null){

			panelToolBar = new JPanel(new FlowLayout( FlowLayout.RIGHT, 5, 2 ));

			panelToolBar.add( getBotaoFechar() );
			panelToolBar.add( getBotaoIncluir() );
			panelToolBar.add( getBotaoEditar() );
			panelToolBar.add( getBotaoExcluir() );
			panelToolBar.add( getBotaoAtualizar() );
			
			panelToolBar.setBorder(BorderFactory.createEtchedBorder());
		}
		return panelToolBar;
	}

	public JPanel getPanelCentral(){
		if(panelCentral == null){

			panelCentral = new JPanel(new BorderLayout());
			panelCentral.add(new JScrollPane(getTablePesquisa()),BorderLayout.CENTER);
			panelCentral.setBorder(BorderFactory.createEtchedBorder());
		}
		return panelCentral;
	}

	public JTable getTablePesquisa(){
		if(tablePesquisa == null){
			tablePesquisa = new JTable(getTableModel()){
				@Override
				public TableCellEditor getCellEditor() {
					return cellEditor;
				};
				@Override
				public boolean getCellSelectionEnabled() {
					return false;
				};
			};
			tablePesquisa.setBackground(Color.WHITE);
			tablePesquisa.setVerifyInputWhenFocusTarget(false);
			tablePesquisa.setFont(new java.awt.Font("Tahoma", 0, 12));
			tablePesquisa.setSurrendersFocusOnKeystroke(false);
			tablePesquisa.setShowHorizontalLines(true);
			tablePesquisa.setShowVerticalLines(true);
			tablePesquisa.setUpdateSelectionOnSort(false);
			tablePesquisa.setVerifyInputWhenFocusTarget(false);
			tablePesquisa.addMouseListener(new MouseListener() {
				@Override
				public void mouseReleased(MouseEvent arg0) {
				}
				@Override
				public void mousePressed(MouseEvent arg0) {
				}
				@Override
				public void mouseExited(MouseEvent arg0) {
				}
				@Override
				public void mouseEntered(MouseEvent arg0) {
				}
				@Override
				public void mouseClicked(MouseEvent arg0) {
					if(arg0.getClickCount() > 1){
						acaoAoFechar();
						getBotaoFechar().doClick();
					}
				}
			});
		}
		return tablePesquisa;
	}
	
	public Window getOwner(){
		return owner;
	}
	
	
	public abstract void getDados();
	
	public abstract void acaoAoFechar();
	
	public abstract TableModel getTableModel();

	public abstract void setList(List<E> lista);

}
